[id].tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { useParams } from 'common'
  2. import { useEffect } from 'react'
  3. import { TableGridEditor } from '@/components/interfaces/TableGridEditor/TableGridEditor'
  4. import { DefaultLayout } from '@/components/layouts/DefaultLayout'
  5. import { EditorBaseLayout } from '@/components/layouts/editors/EditorBaseLayout'
  6. import { TableEditorLayout } from '@/components/layouts/TableEditorLayout/TableEditorLayout'
  7. import { TableEditorMenu } from '@/components/layouts/TableEditorLayout/TableEditorMenu'
  8. import { useTableEditorQuery } from '@/data/table-editor/table-editor-query'
  9. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  10. import { createTabId, useTabsStateSnapshot } from '@/state/tabs'
  11. import type { NextPageWithLayout } from '@/types'
  12. const TableEditorPage: NextPageWithLayout = () => {
  13. const { id: _id, ref: projectRef } = useParams()
  14. const id = _id ? Number(_id) : undefined
  15. const store = useTabsStateSnapshot()
  16. const { data: project } = useSelectedProjectQuery()
  17. const { data: selectedTable, isPending: isLoading } = useTableEditorQuery({
  18. projectRef: project?.ref,
  19. connectionString: project?.connectionString,
  20. id,
  21. })
  22. /**
  23. * Effect: Creates or updates tab when table is loaded
  24. * Runs when:
  25. * - selectedTable changes (when a new table is loaded)
  26. * - id changes (when URL parameter changes)
  27. */
  28. useEffect(() => {
  29. if (selectedTable && projectRef) {
  30. const tabId = createTabId(selectedTable.entity_type, { id: selectedTable.id })
  31. if (!store.tabsMap[tabId]) {
  32. store.addTab({
  33. id: tabId,
  34. type: selectedTable.entity_type,
  35. label: selectedTable.name,
  36. metadata: {
  37. schema: selectedTable.schema,
  38. name: selectedTable.name,
  39. tableId: id,
  40. },
  41. })
  42. } else {
  43. // If tab already exists, just make it active
  44. store.makeTabActive(tabId)
  45. }
  46. }
  47. }, [selectedTable, id, projectRef])
  48. return <TableGridEditor isLoadingSelectedTable={isLoading} selectedTable={selectedTable} />
  49. }
  50. TableEditorPage.getLayout = (page) => (
  51. <DefaultLayout>
  52. <EditorBaseLayout
  53. productMenu={<TableEditorMenu />}
  54. product="Table Editor"
  55. productMenuClassName="overflow-y-hidden"
  56. >
  57. <TableEditorLayout>{page}</TableEditorLayout>
  58. </EditorBaseLayout>
  59. </DefaultLayout>
  60. )
  61. export default TableEditorPage